package pairwise_model;
import java.io.*;
import java.util.*;
public class Pairwise_CUI {
	private HashMap<String,ArrayList<String>> CUI_profile = new HashMap<String,ArrayList<String>>();
	public Pairwise_CUI(String file){ // could either be cui profile, or semantics profile
		ReadCUI_Profile(file);
	}
	public void ReadCUI_Profile(String filename){
		File f= new File(filename);
		FileInputStream fis;
		try {
			fis = new FileInputStream(f);
			InputStreamReader isr=new InputStreamReader(fis);
			BufferedReader br=new BufferedReader(isr);
			String line = br.readLine();
			while(line != null){
				line = line.trim();
				if(line.length()>0)
					build_cui_profile(line);
				line = br.readLine();
			}
			br.close();
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	public void build_cui_profile(String line){
		if(line == null)
			return;
		String s[] = line.split("\t");
		ArrayList<String> cui_list = new ArrayList<String>();
		if(!CUI_profile.containsKey(s[0])){
			for(int i=1; i<s.length; i++)
				cui_list.add(s[i]);
			CUI_profile.put(s[0], cui_list);
		}
	}
	
	public int Common_CUI_Score(String pmid1, String pmid2){
		int score = 0;
		if(!CUI_profile.containsKey(pmid1) || !CUI_profile.containsKey(pmid2))
			return score;
		ArrayList<String> cui_list1 = CUI_profile.get(pmid1);
		ArrayList<String> cui_list2 = CUI_profile.get(pmid2);
		HashMap<String,Integer> hash_cui1 = new HashMap<String,Integer>();
		for(String cui: cui_list1)
			hash_cui1.put(cui, hash_cui1.size());
		for(String cui: cui_list2)
			if(hash_cui1.containsKey(cui))
				score++;
		return score;
	}
}
